package com.hm.shopping.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.su.bean.ServerResponse;
import com.hm.shopping.auto.su.ControllerResult;
import com.hm.shopping.auto.su.R.Restful;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.def.AdvertisingLocationEnum;
import com.hm.shopping.service.AdvertisingService;
import com.hm.shopping.vo.AdvertisingVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月31日 下午4:29:50
 * @version 1.0
 * @describe 广告管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/advertising")
public class AdvertisingApi {

	@Autowired
	private AdvertisingService advertisingService;
	
	private static final String SWWAGER_TAG = "广告信息管理";
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增广告信息", httpMethod = "POST", notes = "com.hm.shopping.api.AdvertisingApi::addAdvertising")
	public ResponseEntity<ServerResponse<Boolean>> addAdvertising(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) AdvertisingVo advertisingVo) {
		log.debug("用户{}新增广告信息", HmSessionFactory.currentUserId());
		advertisingVo.setCreateUser(HmSessionFactory.currentUserId());
		advertisingService.addAdvertising(advertisingVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{advertisingId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取广告信息", httpMethod = "GET", notes = "com.hm.shopping.api.AdvertisingApi::getAdvertising")
	public ResponseEntity<ServerResponse<AdvertisingVo>> getAdvertising(
			@ApiParam(required = true, name = "advertisingId", value = "广告ID") @PathVariable("advertisingId") String advertisingId) {
		log.debug("用户{}获取广告信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(advertisingService.getAdvertising(advertisingId));
	}

	@RequestMapping(value = "/{advertisingId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改广告信息", httpMethod = "PUT", notes = "com.hm.shopping.api.AdvertisingApi::updateAdvertising")
	public ResponseEntity<ServerResponse<Boolean>> updateAdvertising(
			@ApiParam(required = true, name = "advertisingId", value = "广告ID") @PathVariable("advertisingId") String advertisingId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) AdvertisingVo advertisingVo) {
		log.debug("用户{}修改广告信息", HmSessionFactory.currentUserId());
		advertisingVo.setId(advertisingId);
		advertisingService.updateAdvertising(advertisingVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除广告信息", httpMethod = "DELETE", notes = "com.hm.shopping.api.AdvertisingApi::deleteAdvertising")
	public ResponseEntity<ServerResponse<Boolean>> deleteAdvertising(
			@ApiParam(required = true, name = "advertisingId", value = "广告ID") @RequestParam(required = true, name = "advertisingId") String advertisingId) {
		log.debug("用户{}删除广告信息", HmSessionFactory.currentUserId());
		advertisingService.deleteAdvertising(advertisingId);
		return ControllerResult.success(true);
	}
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页广告记录", httpMethod = "GET", notes = "com.hm.shopping.api.AdvertisingApi::queryAdvertising")
	public ResponseEntity<ServerResponse<PageInfo<AdvertisingVo>>> queryAdvertising(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "advertisingLocation", value = "广告位置") @RequestParam(required = false, name = "advertisingLocation") AdvertisingLocationEnum advertisingLocation,
			@ApiParam(required = false, name = "advertisingTitle", value = "广告标题") @RequestParam(required = false, name = "advertisingTitle") String advertisingTitle){
		log.debug("用户{},查询分页广告记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(advertisingService.queryAdvertising(new PageInfoParam(pageNumber, pageSize), advertisingLocation, advertisingTitle));
	}
}
